from collections import defaultdict


class Solution:
    def calcEquation(self, equations: list, values: list, queries: list) -> list:
        self.mid_dic = defaultdict(dict)
        for i in range(len(equations)):
            self.mid_dic[equations[i][0]][equations[i][1]] = values[i]
            self.mid_dic[equations[i][1]][equations[i][0]] = 1 / values[i]
        self.res = []
        for lists in queries:
            try:
                self.search(lists[1], self.mid_dic[lists[0]], 1, {lists[0], })
            except Exception as e:
                print(e)
                pass
            else:
                self.res.append(-1)
        return self.res

    def search(self, target, search_set, mid_res, searched_set):
        if target in search_set:
            self.res.append(mid_res * search_set[target])
            raise StopAsyncIteration
        else:
            for point in search_set:
                if point not in searched_set:
                    mid_set = searched_set.copy()
                    mid_set.add(point)
                    self.search(target, self.mid_dic[point], mid_res * search_set[point], mid_set)


a = Solution()
print(a.calcEquation(equations=[["a", "b"], ["b", "c"]], values=[2.0, 3.0],
                     queries=[["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]]))
